package com.zz.parkingsystem.service.impl;

import com.zz.parkingsystem.dto.ExceptionOrderDTO;
import com.zz.parkingsystem.dto.OrderDTO;
import com.zz.parkingsystem.dto.RefundOrderDTO;
import com.zz.parkingsystem.entity.FundFlow;
import com.zz.parkingsystem.entity.Order;
import com.zz.parkingsystem.mapper.AppealPayMapper;
import com.zz.parkingsystem.mapper.AppealUnpaidMapper;
import com.zz.parkingsystem.mapper.FundFlowMapper;
import com.zz.parkingsystem.mapper.OrderMapper;
import com.zz.parkingsystem.service.PCOrderService;
import com.zz.parkingsystem.vo.ExceptionOrderVO;
import com.zz.parkingsystem.vo.OrderVO;
import com.zz.parkingsystem.vo.RefundOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class PCOrderServiceImpl implements PCOrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AppealUnpaidMapper appealUnpaidMapper;

    @Autowired
    private AppealPayMapper appealPayMapper;

    @Autowired
    private FundFlowMapper fundFlowMapper;


    /**
     * ------------------------------------订单管理--------------------------------
     */
    @Override
    public List<OrderVO> queryAllOrder(OrderDTO orderDTO) {
        return orderMapper.queryAllOrder(orderDTO);
    }

    @Override
    public Long getOrderCount(OrderDTO orderDTO) {
        return orderMapper.getOrderCount(orderDTO);
    }

    @Override
    public String updateOrderStatus(Long orderID) {
        if (orderMapper.updateOrderStatusToPaid(orderID) > 0) {
            return "操作成功";
        } else {
            return "操作失败";
        }
    }

    @Override
    public List<OrderVO> queryAllOrderExcel(OrderDTO orderDTO) {
        return orderMapper.queryAllOrderExcel(orderDTO);
    }

    /**
     * ------------------------------异常订单--------------------------------
     */
    @Override
    public List<ExceptionOrderVO> queryAllExceptionOrder(ExceptionOrderDTO exceptionOrderDTO) {
        return orderMapper.queryAllExceptionOrder(exceptionOrderDTO);
    }

    @Override
    public Long getExceptionCount(ExceptionOrderDTO exceptionOrderDTO) {
        return orderMapper.getExceptionCount(exceptionOrderDTO);
    }

    @Override
    public String updateExceptionStatus(ExceptionOrderDTO exceptionOrderDTO) {
        if (orderMapper.updateExceptionStatus(exceptionOrderDTO.getOiNo(),exceptionOrderDTO.getEoStatus()) > 0) {
            //给异常表update一个处理时间
            orderMapper.updateExceptionHandleTime(exceptionOrderDTO.getOiNo());
            //如果修改的异常订单状态为1：已通过->修改订单项的金额、修改订单表状态为1：未支付、修改未支付申诉表状态为1：已处理
            if (exceptionOrderDTO.getEoStatus() == 1) {
                orderMapper.updateOrderAmountByOiNo(exceptionOrderDTO.getOiNo(), exceptionOrderDTO.getResetAmount());
                orderMapper.updateOrderStatusToUnPaid(new Long(exceptionOrderDTO.getOrderId()));
                appealUnpaidMapper.updateAuStatusByAuId(exceptionOrderDTO.getAuId(), 1);
                //如果修改的异常订单状态为2：不通过->修改订单表状态为1：未支付、修改未支付申诉表状态为2：不通过
            } else {
                orderMapper.updateOrderStatusToUnPaid(new Long(exceptionOrderDTO.getOrderId()));
                appealUnpaidMapper.updateAuStatusByAuId(exceptionOrderDTO.getAuId(), 2);
            }
            return "操作成功";
        } else {
            return "操作失败";
        }
    }

    @Override
    public List<ExceptionOrderVO> queryAllExceptionOrderExcel(ExceptionOrderDTO exceptionOrderDTO) {
        return orderMapper.queryAllExceptionOrderExcel(exceptionOrderDTO);
    }

    /**
     * ------------------------------------------售后订单--------------------------------------
     */
    @Override
    public List<RefundOrderVO> queryAllRefundOrder(RefundOrderDTO refundOrderDTO) {
        return orderMapper.queryAllRefundOrder(refundOrderDTO);
    }

    @Override
    public Long getRefundCount(RefundOrderDTO refundOrderDTO) {
        return orderMapper.getRefundCount(refundOrderDTO);
    }

    @Override
    public String updateRefundStatus(RefundOrderDTO refundOrderDTO) {
        if (orderMapper.updateRefundStatusByRefId(refundOrderDTO.getRefId(),refundOrderDTO.getRefStatus()) > 0) {
            //如果售后订单状态更改为1：已处理->支付申诉表状态改为1：已通过、订单表状态改为4：已退款、资金流水表insert数据
            if(refundOrderDTO.getRefStatus()==1){
                appealPayMapper.updateAPStatusByAPId(refundOrderDTO.getApId(),1);
                orderMapper.updateOrderStatusToRefund(new Long(refundOrderDTO.getOrderId()));
                FundFlow fundFlow = new FundFlow();
                fundFlow.setOiId(refundOrderDTO.getOiId());
                fundFlow.setPaidType(refundOrderDTO.getPtType());
                fundFlow.setFundType(1);
                fundFlow.setOperatorAmount(refundOrderDTO.getOiAmount());
                fundFlow.setOperatorTime(new Date());
                fundFlowMapper.insert(fundFlow);
                //如果售后订单状态更改为2：不通过->支付申诉表状态改为2：不通过、订单表状态改为2：已付款
            } else {
                appealPayMapper.updateAPStatusByAPId(refundOrderDTO.getApId(),2);
                orderMapper.updateOrderStatusToPaid(new Long(refundOrderDTO.getOrderId()));
            }
            return "操作成功";
        } else {
            return "操作失败";
        }
    }

    @Override
    public List<RefundOrderVO> queryAllRefundOrderExcel(RefundOrderDTO refundOrderDTO) {
        return orderMapper.queryAllRefundOrderExcel(refundOrderDTO);
    }
}
